import {Component, Input, OnInit} from '@angular/core';
import {Observable} from "rxjs";
import {Student} from "../student";
import {StudentService} from "../student-detail/student.service";

@Component({
  selector: 'app-student-datagrid',
  templateUrl: './student-datagrid.component.html',
  styleUrls: ['./student-datagrid.component.css']
})
export class StudentDatagridComponent implements OnInit {
  pageNum:number=1
  pages:number
  @Input() pageSize:number=2
  students:Student[]
  constructor(private studentService:StudentService) { }

  ngOnInit() {
    this.getData()
  }
  up(){
    if(this.pageNum >1)
    this.pageNum--
    this.getData()
  }
down(){
    if(this.pageNum <this.pages)
    this.pageNum++
    this.getData()
}
change(){
    this.pageNum=1
    this.getData()
}
  getData(){
      this.studentService.list(this.pageNum,this.pageSize).subscribe(pageInfo=>{
      this.pages=Math.ceil(pageInfo.total / this.pageSize)
      this.students=pageInfo.rows
    })
  }
}
